myApp.factory('$serSign',function ($resource,URL,$localStorage) {
    var setSend = function(data, url){
        data = data || {};
        url = URL + url;
        return $resource(url)
            .save(data)
            .$promise;
    };
    var userInfoIn = function (res) {
        $localStorage.userToken = res.data.token;
        $localStorage.userId = res.data.vendorId;
        $localStorage.userState = res.code;
        $localStorage.userInfo = res.data;
    };
    var userInfoOut = function (res) {
        $localStorage.userToken=null;
        $localStorage.userId = null;
        $localStorage.userState = null;
        $localStorage.userInfo =null;
    };
    return {
        clearUserInfo:function () {
            userInfoOut();
        },
        /*登录*/
        login: function(data){
            var res = setSend(data,'Login/login');
            res.then(function(res){
                var code = res.code;
                if(code==1||code==-4||code==-7||code==-8){
                    userInfoIn(res);
                    return res;
                }else {
                    userInfoOut(res);return res;
                }
            });
            return res;
        },
        /*注册*/
        register: function(data){
            var res = setSend(data,'Register/addPhoneAccount');
            res.then(function(res){
                switch(res.code){
                    case 1:userInfoIn(res);return res;break;
                    default :userInfoOut(res);return res;break;
                }
            });
            return res;
        },
        /*注册验证码*/
        registerVerification: function(data){
            return setSend(data,'Register/sendVerificationCode');
        },
        /*完善信息*/
        information: function(data){
            data = angular.extend(data);
            return setSend(data,'Vendor/saveInfo');
        },
        /*用户认证*/
        verify: function(data){
            return setSend(data,'Vendor/verify');
        }
    }
});